<template>
    <div style='width:100%;text-align:center;overflow:hidden'>
       <img id="bigPriDev" width="640" height="480"  :src="src"/>
       <div style='width:100%;text-align:left;padding-left:20px;' class='bottoms'>
           <span style='cursor:pointer;margin-right:100px' @click="changeVideo" >摄像头不对？点击切换</span>
           
           <el-button type="primary" @click="scan" style='margin-left:100px'>上传</el-button>
           <el-button type="primary" @click="scan" v-if="names.length<=1">上传</el-button>
            <template v-else>
                <el-button type="primary" style='margin-top:5px' class="uploads" @click="changeName(item)"  v-for="(item,index) in names" :key="index">{{item}}</el-button>
            </template>
           <el-button type="primary" @click="closes()" >关闭</el-button>
       </div>
   </div>
</template>
<script>
export default {
   name:"high-shotlt",
   props:["show","appid","showType","params"],
   data(){
       return{
           webSocket:null,
           src:"http://127.0.0.1:38088/video=stream&camidx=0",
           timer:null,
           isVideo:"0",
           img:"",
           name:"",
           names:[],
           isCJ:"Y"
       }
   },
   created(){
    try{
            if(this.params&&this.params.names){
                this.names=this.params.names.split(",");
            }
            if(this.names.length==1){
                this.name=this.names[0]
            }
            if(this.names.length==0){
                this.name=tis.params.name;
            }
        }catch(e){}
        try{
            if(this.params&&this.params.isCJ){
                this.isCJ=this.params.isCJ
            }
        }catch(e){}
   },
   mounted(){
      /* let data = {
           "camidx": "0",
           "open": "1"
       }
       var ajax = new XMLHttpRequest();
       //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
      
       ajax.open('post','http://127.0.0.1:38088/dvideo=cameradeskew');
       //步骤三:发送请求
       ajax.send(JSON.stringify(data));

       ajax.onreadystatechange = function () {
       if (ajax.readyState==4 &&ajax.status==200) {
           //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
       　　　　console.log(ajax.responseText,"cameradeskew------------------------");//输入相应的内容
               
       　　}
       }*/
   },
   methods:{
        changeName(item){
            this.name=item;
            this.scan();
        },
       changeVideo(){
           
           if(this.src=="http://127.0.0.1:38088/video=stream&camidx=0"){
               this.isVideo="1";
               this.src="http://127.0.0.1:38088/video=stream&camidx=1";
           }else{
               this.isVideo="0";
               this.src="http://127.0.0.1:38088/video=stream&camidx=0";
           }
       },
       scan(){
           let that=this;
            let data = {
               "filepath": "base64",
               "rotate": "0",
               "deskew": "0",
               "deskewval":"0",
               "camidx": this.isVideo,
               "ColorMode": "0",
               "quality": "0"
           }
           if(this.isCJ=="Y"){
                data = {
                "filepath": "base64",
                "rotate": "0",
                "deskew": this.isVideo=='0' ? "1":"0",
                "deskewval":"-1",
                "camidx": this.isVideo,
                "ColorMode": "0",
                "quality": "0"
            }
           }
           
               /*"deskew": this.isVideo=='0' ? "1":"0",*/
          /* $.post("http://127.0.0.1:38088/video=grabimage", JSON.stringify(data), function(res){
               my_log("base64:" + res.photoBase64, 1);
               my_log("拍照成功");
               add_img(res.photoBase64);
           })*/
          /* let url="http://127.0.0.1:38088/video=grabimage";
           let that=this;
           var ajax = new XMLHttpRequest();
           //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
           ajax.open('post',url);
           ajax.setRequestHeader("Content-type", "application/json");
           //步骤三:发送请求
           ajax.send(JSON.stringify(data));
           //步骤四:注册事件 onreadystatechange 状态改变就会调用
           */
            var ajax = new XMLHttpRequest();
           //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
           let params='filepath=base64&rotate=0&deskew=0&camidx='+this.isVideo+"&ColorMode=0&quality=0";
           ajax.open('post','http://127.0.0.1:38088/video=grabimage');
           //步骤三:发送请求
           ajax.send(JSON.stringify(data));

           ajax.onreadystatechange = function () {
           if (ajax.readyState==4 &&ajax.status==200) {
               //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
           　　　　console.log(ajax.responseText,"readCard------------------------");//输入相应的内容
                   let data=JSON.parse(ajax.responseText);
                  that.img=data.photoBase64;
                  that.uploads();
           　　}
           }
       },

       uploads(){
          let name=this.params.name;
          this.$post(this.appid,"/api/file/fileUploadBase64",{data:this.img.replace("data:image/jpeg;base64,",""),model:this.params.model,type:this.params.type,id:this.params.id,token:this.params.token,name:this.name}).then(data=>{
               console.log("uploadImg==========================");
               console.log(data);
               this.$emit("uploadImg",JSON.stringify(data))
           });
       },
       closes(){
          
           this.$emit("closes","");
       }
   }
}
</script>